﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.Result;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0201", Name = "年号检证_非常规年号", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0201:VerfityOneResult
    { 

        [Category("检查对象的元年"), Description("设置要检查的元年")]
        public EnumGannnenn VerfityGannNenn
        {
            get { return _verfityGannNenn; ; }
            set { _verfityGannNenn = value; }
        }

        [Category("检查对象的检查方式"), Description("设置要检查方式（true:等于,false:不等于）")]
        public bool Equal { get; set; }

        private EnumGannnenn _verfityGannNenn;

        

        string _formes = string.Empty;

        [Category("检查的Form"), Description("设置要检查的Form")] 
        [EditorAttribute(typeof(PropertyGridRichText),typeof(System.Drawing.Design.UITypeEditor))]
        public String Forms
        {
            get { return _formes; }
            set { _formes = value; }
        }

        private IEnumerable<string> FormArray
        {
            get {

                return  _formes.Replace("\n",string.Empty).Split(',');
            }
        }

        /// <summary>
        /// 2013 9 27 修改
        /// 元号为空白时候 不判断
        /// 
        /// 原条件:
        /// 元号是设定参数以外的数字时(取第一组年号即可)
        /// 抽出检证（限定Form）
        /// 追加条件：
        /// 1.第一组的[年]有记载时
        /// 2.第一组的[年]不是西历时
        /// </summary>
        /// <param name="result"></param>
        /// <param name="entryCommon"></param>
        /// <returns></returns>
        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
    
            //List<string> entryResults = result.Select(t => (t == null || t.Count == 0) ? string.Empty : t[0]).ToList();
            //如果是多列，首位相接排列
            List<string> entryResults = result.SelectMany(o => o).Where(t => EmptyResultCount(t).Equals(0)).ToList();

            if (entryResults.Count == 0)
                return false;

            //得到第一组日期
            //只检查第一组日期
            var entryResult = entryResults[0];

  
            //原来 循环所有结果
            //现在 只判断第一组结果
            //foreach (var entryResult in checkResults)
            //{
                //System.Console.WriteLine(string.Format("formid:{0} result:{1}", ChildImageObject.FormId, entryResult));

                //检查 formid
                if (ChildImageObject.FormId != null)
                {
                    //formid 检查失败
                    if (!CheckFomrId(ChildImageObject.FormId))
                    {
                        return false;
                        //break;
                    }                     
                }
                

                //得到年号位置
                List<string>   arrayDateFormat = ChildImageObject.CategorySetting.DataFormat.Split(new char[] {'/'}).ToList();

                int indexGannNenn = arrayDateFormat.IndexOf("gg");
                int indexYear =  arrayDateFormat.IndexOf("yy");

                if (indexGannNenn > -1)
                {
                    //拆分结果
                    List<string> entry = entryResult.Split(new char[] { '/' }).ToList();

                    string gannNenn = entry[indexGannNenn];
                    string year = entry[indexYear];


                    if (!string.IsNullOrEmpty(gannNenn) 
                        && !gannNenn.Contains("?")
                        && !year.Length.Equals(4))
                    {
                        EnumGannnenn entryGannnenn = Util.DateStrHelper.GetGannnenn(gannNenn);
                        // Enum.TryParse(year, out gannnenn);

                        //判断
                        if (this.Equal)
                        {
                            bresult = ((VerfityGannNenn & entryGannnenn) == entryGannnenn);
                        }
                        else
                        {
                            bresult = ((VerfityGannNenn & entryGannnenn) != entryGannnenn);
                        }
                    }
                    //年是西历，年号为空的时候不抽出
                    else if (string.IsNullOrEmpty(gannNenn) && year.Length.Equals(4))
                    {
                        return false;
                    }
                    else if (string.IsNullOrEmpty(gannNenn))
                    {
                        return true;
                    }
                }
                
                //if (bresult)
                //    break;
            //}
  

            if (bresult)
            {
                System.Console.WriteLine("VerfityRq0201");
            }

            return bresult;   
        }
         

        private bool CheckFomrId(string childImageFormId)
        {
            if (String.IsNullOrEmpty(childImageFormId) && !string.IsNullOrEmpty(_formes))
                return false;

            if (_formes.Length == 0)
            {
                return true;
            }
            else
            {

                return FormArray.Contains(childImageFormId);
            }

        }

    }
}